<?php  
 die;
	$connect=mysqli_connect('localhost','root','wlxiw#983kckwz8lai','heshui','3306');
	//2.定义sql语句
	$now_time=time();
	$thi_order_table='hs_order_9';
	$sql="SELECT *  FROM ( SELECT id,userID,uuid,amount,COUNT(*) AS nums FROM ".$thi_order_table."  WHERE uuid IS NOT NULL AND updateTime >1533052800 and isValid=1 GROUP BY uuid,userId,amount ) mb  WHERE mb.nums>1";//找出所有重复出现uuid和价格的订单
	mysqli_query($connect,'set names utf8');
	//3.发送SQL语句
	$result=mysqli_query($connect,$sql);
	if($result){
		foreach($result as $v){ 
			$need_add_money=0;
			//--计算金额----
			$need_add_money=$v['amount']*($v['nums']-1);  
			//--处理无效---
			update_user_data($need_add_money,$v['userID'],$connect,$now_time);
			update_invalid_order($thi_order_table,$v['id'],$v['userID'],$v['uuid'],$connect,$now_time);
		}
	}
	die;
	function update_user_data($need_add_money=0,$uid,$connect,$now_time){
		$sql="SELECT id,balance,buyTotal  FROM hs_user where id =".$uid;//找出该用户
		mysqli_query($connect,'set names utf8'); 
		$result=mysqli_query($connect,$sql);
		$user_data=array();
		if($result){
			foreach($result as $v){ 
				$user_data=$v; 
			}
		}
		$old_balance=$user_data['balance'];
		$old_buy=$user_data['buyTotal'];
		
		$new_balance=$old_balance+$need_add_money;
		$new_buy=$old_buy-$need_add_money; 
		$update_sql='update hs_user set balance='.$new_balance.' , buyTotal='.$new_buy.' ,updateTime='.$now_time.'  where id='.$uid;
		$result_update=mysqli_query($connect,$update_sql);
		if($result_update){
			echo $uid.' add '.$need_add_money;
			echo "\n";
		}else{
			echo $update_sql;
			echo "\n";
		}
	}
	function update_invalid_order($thi_order_table,$thisid,$thisuid,$thisuuid,$connect,$now_time){
		$update_sql="update ".$thi_order_table." set isValid=0 ,updateTime=".$now_time." where id<>".$thisid." and userID=".$thisuid." and uuid='".$thisuuid."' ";//找出该用户  
		$result_update=mysqli_query($connect,$update_sql);
		if($result_update){
			echo $thisuuid.' is remove double';
			echo "\n";
		}else{
			echo $update_sql;
			echo "\n";
		}
	} 
?>
 
 
